﻿using Business.Base;
using Business.Base.Common;
using Business.Base.Enums;
using Business.Base.Model;
using Business.Base.Service;
using Business.CentrifugalPump.Perf;

using Configuration;
using System;
using System.Collections.Generic;

using System.Windows.Forms;

namespace Pumptester.ExperimentForms.CentrifugalPump.Perf
{
    public partial class CreateForm : Pumptester.ExperimentForms.Base.BaseCreateForm
    {
        const ExperimentType experimentType = ExperimentType.CentrifugalPumpPerf;


        protected override void BandCombox()
        {
            Dictionary<ComboBox, DataSource[]> boxDic = new Dictionary<ComboBox, DataSource[]>();

            boxDic.Add(comboBoxFlowmeter, DataSource.DataSourceDic[DataSource.FLOWMETER]);
            boxDic.Add(comboBoxWindingConnection, DataSource.DataSourceDic[DataSource.WINDINGCONNECTION]);
            boxDic.Add(comboBoxWindingMaterial, DataSource.DataSourceDic[DataSource.WINGDINGMATERIAL]);
            boxDic.Add(comboBoxLevel, DataSource.DataSourceDic[DataSource.FLOWHEADLEVEL]);
            boxDic.Add(comboBoxRateVoltage, DataSource.DataSourceDic[DataSource.MOTORRATEDVOLTAGE]);

            foreach (KeyValuePair<ComboBox, DataSource[]> pair in boxDic)
            {
                pair.Key.DataSource = pair.Value;
                pair.Key.DisplayMember = "BindMember";
                pair.Key.ValueMember = "BindValue";
                pair.Key.SelectedIndex = 0;
            }

            //this.comboBoxPumpType.SelectedValue = pumpType;
        }

        protected override void initValidateInfo()
        {
            notNullList = new List<TextBox>();
            doubleFormateList = new List<TextBox>();
            doubleorNullFormateList = new List<TextBox>();

            intList = new List<TextBox>();

            notNullList.Add(this.textBoxCode);
            notNullList.Add(this.textBoxPumpName);
            notNullList.Add(this.textBoxPumpCode);
            notNullList.Add(this.textBoxPumpCode);
            notNullList.Add(this.textBoxExperimentOwner);
            notNullList.Add(this.textBoxManufacturer);
            notNullList.Add(this.textBoxRatedFlow);
            notNullList.Add(this.textBoxRatedHead);
            notNullList.Add(this.textBoxRatedPower);
            notNullList.Add(this.textBoxDInlet);
            notNullList.Add(this.textBoxDOutlet);
            notNullList.Add(this.textBoxImpellerDia);
            notNullList.Add(this.textBoxRatedSpeed);
            notNullList.Add(this.textBoxSynchronousSpeed);
            notNullList.Add(this.textBoxRatedVoltage);
            notNullList.Add(this.textBoxRatedCurrent);
            notNullList.Add(this.textBoxCurrentTransRatio);
            notNullList.Add(this.textBoxMotorEff);
            notNullList.Add(this.textBoxZInLet);
            notNullList.Add(this.textBoxZOutLet);

            doubleFormateList.Add(this.textBoxRatedFlow);
            doubleFormateList.Add(this.textBoxRatedHead);
            doubleFormateList.Add(this.textBoxRatedPower);
            doubleFormateList.Add(this.textBoxDInlet);
            doubleFormateList.Add(this.textBoxDOutlet);
            doubleFormateList.Add(this.textBoxRatedSpeed);
            doubleFormateList.Add(this.textBoxSynchronousSpeed);
            doubleFormateList.Add(this.textBoxRatedVoltage);
            doubleFormateList.Add(this.textBoxRatedCurrent);
            doubleFormateList.Add(this.textBoxImpellerDia);

            doubleFormateList.Add(this.textBoxZInLet);
            doubleFormateList.Add(this.textBoxZOutLet);

            doubleFormateList.Add(this.textBoxMotorEff);
            doubleFormateList.Add(this.textBoxZInLet);
            doubleFormateList.Add(this.textBoxZOutLet);

            //doubleorNullFormateList.Add(this.textBoxBenchmarkWorkingTemperature);
            //doubleorNullFormateList.Add(this.textBoxStrayLossRate);

            intList.Add(this.textBoxImpellerDia);
            //intList.Add(this.textBoxCurrentTransRatio);

            //Xyj 添加验证逻辑
            //floatValidateList.Add();

        }

        protected override Experiment buildExperiment()
        {
            CentrifugalPumpPerfExperiment experiment = new CentrifugalPumpPerfExperiment();
            experiment.ExperimentCode = this.textBoxCode.Text;
            experiment.PumpName = this.textBoxPumpName.Text;
            //experiment.ExperimentName = this.textBoxPumpName.Text;
            experiment.PumpModel = this.textBoxProductModel.Text;
            experiment.PumpCode = this.textBoxPumpCode.Text;
            experiment.ExperimentOwner = this.textBoxExperimentOwner.Text;
            experiment.Manufacturer = this.textBoxManufacturer.Text;
            

            experiment.PumpRatedFlow = Convert.ToDouble(this.textBoxRatedFlow.Text);
            experiment.PumpRatedHead = Convert.ToDouble(this.textBoxRatedHead.Text);
            experiment.PumpRatedPower = Convert.ToDouble(this.textBoxRatedPower.Text) ;
            experiment.PumpRatedEfficiency = Convert.ToDouble(this.textBoxRatedEff.Text);

            //experiment.DesignStages = (int)Convert.ToInt32(this.textBoxImpellerDia.Text);
            experiment.MotorRatedSpeed = Convert.ToDouble(this.textBoxRatedSpeed.Text);
            //experiment.DesignSynchronousSpeed = Convert.ToDouble(this.textBoxSynchronousSpeed.Text);
            experiment.ImpellerDia = Convert.ToDouble(this.textBoxImpellerDia.Text)/1000;
            experiment.MotorRatedVoltage =  (MotorRatedVoltage) Convert.ToInt32(this.comboBoxRateVoltage.SelectedValue);
            experiment.MotorRatedCurrent = Convert.ToDouble(this.textBoxRatedCurrent.Text);
            //experiment.MotorRatedEfficiency = Convert.ToDouble(this.textBoxRatedEff.Text);
            experiment.MotorWindingConnection = (string)this.comboBoxWindingConnection.SelectedValue;
            experiment.MotorWindingMaterial =(String)this.comboBoxWindingMaterial.SelectedValue;

            experiment.MotorRatedEfficiency = Convert.ToDouble(this.textBoxMotorEff.Text);

            experiment.ZInlet = Convert.ToDouble(this.textBoxZInLet.Text) ;
            experiment.ZOutlet = Convert.ToDouble(this.textBoxZOutLet.Text)    ;


            experiment.DInlet = Convert.ToDouble(this.textBoxDInlet.Text)/1000;
            experiment.DOutlet = Convert.ToDouble(this.textBoxDOutlet.Text)/1000;

            experiment.ZInlet = Convert.ToDouble(this.textBoxZInLet.Text)/1000 ;
            experiment.ZOutlet = Convert.ToDouble(this.textBoxZOutLet.Text)/1000;
            
            

            experiment.Flowmeter = comboBoxFlowmeter.Text;
            experiment.FlowmeterAddress = (int)comboBoxFlowmeter.SelectedValue;
            experiment.ExperimentType = ExperimentType.CentrifugalPumpPerf;
            experiment.ExperimentLevel = (FlowHeadLevel)this.comboBoxLevel.SelectedValue;
            experiment.CreateTime = DateTime.Now;
            //experiment.Flowmeter = this.comboBoxFlowmeter.SelectedText;
            experiment.FlowmeterAddress = (int)this.comboBoxFlowmeter.SelectedValue;
            experiment.TransformationRatio = Convert.ToDouble( this.textBoxCurrentTransRatio.Text);
            //experiment.PumpType = pumpType;//普通离心泵
            experiment.Customer = this.textBoxCustomer.Text;

            return experiment;

            //Experiment = experiment;
        }

        public CreateForm() 
        {
            
            InitializeComponent();
            this.textBoxCode.Text = ExperimentService.GenerateExperimentCode(experimentType); 
        }

        private void btnSure_Click(object sender, EventArgs e)
        {
  
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            
        }

        protected override String RecentRecordFilePath => Config.BasePath + "\\" + experimentType  + "-recent.obj";

        protected override void assignRecentRecordToFace(Experiment experiment)
        {
            CentrifugalPumpPerfExperiment centrifugalPumpPerfExperiment = (CentrifugalPumpPerfExperiment)experiment;

            this.textBoxPumpName.Text = centrifugalPumpPerfExperiment.PumpName;

            this.textBoxProductModel.Text = centrifugalPumpPerfExperiment.PumpModel;
            this.textBoxPumpCode.Text = centrifugalPumpPerfExperiment.PumpCode;
            this.textBoxExperimentOwner.Text = centrifugalPumpPerfExperiment.ExperimentOwner;
            this.textBoxManufacturer.Text = centrifugalPumpPerfExperiment.Manufacturer;

           // this.textBoxRatedFlow.Text = centrifugalPumpPerfExperiment.DesignFlow.ToString();
           // this.textBoxRatedHead.Text = centrifugalPumpPerfExperiment.DesignHead.ToString();
           // this.textBoxRatedPower.Text = (centrifugalPumpPerfExperiment.DesignShaftPower / 1000.0).ToString();

            this.textBoxRatedFlow.Text = centrifugalPumpPerfExperiment.PumpRatedFlow.ToString();
            this.textBoxRatedHead.Text = centrifugalPumpPerfExperiment.PumpRatedHead.ToString();
            this.textBoxRatedPower.Text = (centrifugalPumpPerfExperiment.PumpRatedPower).ToString();
            this.textBoxRatedSpeed.Text = centrifugalPumpPerfExperiment.MotorRatedSpeed.ToString();
            this.textBoxImpellerDia.Text = (centrifugalPumpPerfExperiment.ImpellerDia*1000).ToString();

            this.textBoxRatedCurrent.Text = centrifugalPumpPerfExperiment.MotorRatedCurrent.ToString();
            this.textBoxRatedEff.Text = centrifugalPumpPerfExperiment.PumpRatedEfficiency.ToString();

            this.textBoxMotorEff.Text = centrifugalPumpPerfExperiment.MotorRatedEfficiency.ToString();
            this.textBoxDInlet.Text = (centrifugalPumpPerfExperiment.DInlet * 1000).ToString();
            this.textBoxDOutlet.Text = (centrifugalPumpPerfExperiment.DOutlet * 1000).ToString();
            this.textBoxZInLet.Text = (centrifugalPumpPerfExperiment.ZInlet * 1000).ToString();
            this.textBoxZOutLet.Text = (centrifugalPumpPerfExperiment.ZOutlet * 1000).ToString();

            this.comboBoxRateVoltage.SelectedValue = (int)centrifugalPumpPerfExperiment.MotorRatedVoltage;
            comboBoxFlowmeter.SelectedValue = centrifugalPumpPerfExperiment.FlowmeterAddress;
            this.comboBoxWindingConnection.SelectedValue = centrifugalPumpPerfExperiment.MotorWindingConnection;
            this.comboBoxWindingMaterial.SelectedValue = centrifugalPumpPerfExperiment.MotorWindingMaterial;

            this.comboBoxLevel.SelectedValue = centrifugalPumpPerfExperiment.ExperimentLevel;
            this.textBoxCurrentTransRatio.Text = centrifugalPumpPerfExperiment.TransformationRatio.ToString();


            this.textBoxCustomer.Text = centrifugalPumpPerfExperiment.Customer;
        }

        private void CreateForm_Load(object sender, EventArgs e)
        {

        }

       

        private void changeRatedCurrent()
        {
            try
            {
                double designedPower = Convert.ToDouble(this.textBoxRatedPower.Text);
                double ratedVoltage = Convert.ToDouble(this.comboBoxRateVoltage.SelectedValue);
                double transRadio = Config.TransFormationRadioConfig[ratedVoltage, designedPower]; 
                if (!Double.IsNaN(transRadio))
                {
                    this.textBoxCurrentTransRatio.Text = transRadio.ToString();
                }
                else
                {
                    this.textBoxCurrentTransRatio.Text = "";
                }
            }
            catch (FormatException ex)
            {
                ex.GetType();
                Console.WriteLine(ex.ToString());
            }
        }
            

        private void textBoxRatedPower_Leave(object sender, EventArgs e)
        {
            changeRatedCurrent();
        }

        private void comboBoxRateVoltage_SelectedIndexChanged(object sender, EventArgs e)
        {
            changeRatedCurrent();
        }
    }
}
